Repository navigation
Conversation
…re chunks Avoid redundant work in multi-chunk QSA group selection while staying bit-exact: 1. Cap _scratch_layout workspace_q_rows at min(max_q_rows, 2048). For max_q_rows >= 4096 within the default 128 MiB score workspace, this keeps score_workspace_width at 16384 (5 chunks for 262k context instead of 9 at 4096, 19 at 8192, or 43 at 16384) and shrinks auxiliary scratch tensors. 2. Keep canonical running top-k winners in buffer A across chunks so chunks with no newly eligible groups (group_offset > 0 and chunk_eligible == 0) early-exit across _stage_topk_carry_kernel, _score_representatives_tiled_kernel (merge_lengths=0), DSATiledTopkKernel (skip_leq_topk=True), StableSelectionKernel (preserve_empty_carry=True), and _copy_stable_topk_kernel. 3. Preserve default behavior (skip_empty_chunks=False, skip_leq_topk=False, preserve_empty_carry=False) on standalone helper entry points so existing unit tests and callers keep unchanged contracts.
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 19 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
Author
|
@coderabbitai review |
Action not completed
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Eliminate redundant multi-chunk work in QSA group selection while preserving exact bitwise selection results:
Cap
workspace_q_rowsatmin(max_q_rows, 2048)inqsa._contract._scratch_layout:workspace_q_rows = max_q_rowsnarrowsscore_workspace_widthas prefill chunk size grows: at 262k max context (max_groups = 65536,group_budget = 512),max_q_rows = 4096required 9 score chunks,8192required 19 chunks, and16384required 43 chunks.workspace_q_rowsat 2048 keepsscore_workspace_width = 16384(5 chunks for 262k context) regardless ofmax_q_rows, shrinks auxiliary scratch buffers (topk_values,topk_indices,stable_topk,prepared_query), and reuses the existingfor row_start in range(0, rows, work_rows)chunk loop in_qsa_decode_impl.Canonical buffer A + early-exit across empty score chunks:
chunk_topk_values,chunk_topk_ids) across score chunks rather than ping-ponging A/B, writing per-chunk radix scratch into buffer B forscore_chunk > 0and scattering final stabilized rows back to buffer A.group_offset > 0andchunk_eligible == 0):_stage_topk_carry_kernelexits immediately wheneligible <= GROUP_OFFSET._score_representatives_tiled_kernel(withskip_empty_chunks=True) writesmerge_lengths = 0.DSATiledTopkKernel(withskip_leq_topk=True) skips rows withlength <= topkbecauseStableSelectionKernelhandleslength <= budgetdirectly.StableSelectionKernel(withpreserve_empty_carry=True, compile spec version3 -> 4) and_copy_stable_topk_kernel(PRESERVE_EMPTY_CARRY=True) exit without touching canonical buffer A, preserving the prior chunk's exact winners.skip_empty_chunks,skip_leq_topk,preserve_empty_carry) default toFalseon standalone helper entry points so existing callers and unit tests retain unchanged contracts.Benchmark (NVIDIA GB10 / DGX Spark, SM 12.1)
End-to-end Nsight Systems profile of a 32,768-token Qwen3.8 Flash-Next prefill on GB10 (all 4 selector kernels combined:
_score_representatives_tiled,DSATiledTopk,StableSelection,_copy_stable_topk+_stage_topk_carry):447.5 ms -> 139.0 ms(-308.5 ms,-69.0%) vs stock 4K-chunk baseline.Testing
test_qsa_score_workspace_bound_sets_selection_chunksintests/experimental/b12x/attention/test_qsa_contract.pyfor the 2048-row workspace cap (includingmax_q_rows=8192).test_stable_selection_preserve_empty_carry_keeps_dest_untouchedintests/experimental/b12x/attention/test_qsa_stable_selection.py.pytest --confcutdir=tests/experimental/b12x tests/experimental/b12x/attention/test_qsa_stable_selection.py tests/experimental/b12x/attention/test_qsa_program_keys.py tests/experimental/b12x/attention/test_qsa_fixed_splits_gpu.py tests/experimental/b12x/attention/test_qsa_contract.py -k "score_workspace_bound or tiled_scores_match_scalar or tiled_representative or multi_chunk or select_only or stable_selection or preserve_empty or fixed_splits"-> 46 passed.pytest --confcutdir=tests/experimental/b12x tests/experimental/b12x/attention/test_paged_prefill_topk_long_context.py-> 11 passed.